﻿using MediatR;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
using StoreIGet.Applications.Command;
using StoreIModels;
using StoreRepositorys.Interface;

namespace StoreIGet.Applications.CommandHandler
{
    public class StoreGetCommandHandler : IRequestHandler<StoreGetCommand, List<StoreModel>>
    {
        public readonly IStoreRepository _storeRepository;

        public StoreGetCommandHandler(IStoreRepository storeRepository)
        {
            _storeRepository = storeRepository;
        }

     

       public async  Task<List<StoreModel>>Handle(StoreGetCommand request, CancellationToken cancellationToken)
        {
            var res = from x in await _storeRepository.GetAllasync()
                      where (string.IsNullOrEmpty(request.StoreState) || x.StoreState == request.StoreState)
                      && (request.AddressProvince == 0 || x.AddressProvince == request.AddressProvince)
                      && (request.AddressCity == 0 || x.AddressCity == request.AddressCity)
                      && (request.AddressCounty == 0 || x.AddressCounty == request.AddressCounty)
                      && (string.IsNullOrEmpty(request.Men) || x.StoreName.Contains(request.Men))
                      select x;
            return res.ToList();

            
        }
    }
}
